/* ========================================================================
 * PROJECT: FTIR Blob Tracker
 * ========================================================================
 * This work is based on the original FTIR Blob Tracker developed by
 *   Woonhyuk Baek
 *   Woontack Woo
 *   U-VR Lab, GIST of Gwangju in Korea.
 *   
 *   http://uvr.gist.ac.kr/
 *
 * Copyright of the derived and new portions of this work
 *     (C) 2009 GIST U-VR Lab.
 *
 * This framework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this framework; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * For further information please contact 
 *   Woonhyuk Baek
 *   <windage@live.com>
 *   GIST U-VR Lab.
 *   Department of Information and Communication
 *   Gwangju Institute of Science and Technology
 *   1, Oryong-dong, Buk-gu, Gwangju
 *   South Korea
 * ========================================================================
 ** @author   Woonhyuk Baek
 * ======================================================================== */

#include "Pipelining.h"

std::string Pipelining::GetFilterName(int at)
{
	if((int)this->filterList.size() > at)
	{
		return this->filterList[at]->GetName();
	}
	else
	{
		return std::string("");
	}
}
Filter* Pipelining::GetFilter(int at)
{
	if((int)this->filterList.size() > at)
	{
		return this->filterList[at];
	}
	else
	{
		return NULL;
	}
}

int Pipelining::PushBack(Filter* filter)
{
	int count = this->GetSize();
	if(count > 0)
	{
		IplImage* tempImage = cvCreateImage(cvSize(width, height), depth, 1);
		this->temporaryImage.push_back(tempImage);
		
		this->GetFilter(count-1)->SetOutputImage(tempImage);

		filter->SetInputImage(tempImage);
		filter->SetOutputImage(this->outputImage);
	}
	else
	{
		filter->SetInputImage(this->inputImage);
		filter->SetOutputImage(this->outputImage);
	}

	this->filterList.push_back(filter);
	return 0;
}

Filter* Pipelining::PopBack()
{
	int count = this->GetSize();
	if(count > 1)
	{
		this->GetFilter(count-2)->SetOutputImage(this->outputImage);

		IplImage* tempImage = this->temporaryImage[this->temporaryImage.size()-1];
		this->temporaryImage.pop_back();
		cvReleaseImage(&tempImage);
		
		Filter* returnFilter = this->filterList[count-1];
		returnFilter->SetInputImage(NULL);
		returnFilter->SetOutputImage(NULL);
		this->filterList.pop_back();
		return returnFilter;
	}
	else if(count == 1)
	{
		Filter* returnFilter = this->filterList[count-1];
		returnFilter->SetInputImage(NULL);
		returnFilter->SetOutputImage(NULL);
		this->filterList.pop_back();
		return returnFilter;
	}
	else
		return NULL;
}

int Pipelining::Do()
{
	int count = this->GetSize();
	for(int i=0; i<count; i++)
	{
		this->filterList[i]->Do();
	}

	return 0;
}